package com.shwread.android.qysw.step.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shwread.android.qysw.activity.healthy.GroupPKDetailActivity;
import com.shwread.android.qysw.step.dto.HMMatch;
import com.shwread.android.qysw.step.dto.LqswHealthMatch;
import com.shwread.android.qysw.step.dto.Player;
import com.shwread.android.qysw.step.model.UserHealthyModel;
import com.shwread.android.qysw.step.view.RectImageView;
import com.shwread.android.qysw.tools.Screen;
import com.shwread.android.qysw.tools.TimeHelper;
import com.shwread.android.qysw10000243.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Fantasy on 16/4/2.
 */
public class GroupGamePKAdapter extends LoadMoreListViewAdapter {
    Context mContext;
    private int isNeedStep = 0;//0不需要,1需要

    public GroupGamePKAdapter(Context context, int isNeedStep) {
        this.mContext = context;
        this.isNeedStep = isNeedStep;
    }

    @Override
    public RecyclerView.ViewHolder subOnCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.h_group_game_item, null);
        rootView.setLayoutParams(new RecyclerView.LayoutParams(Screen.getScreenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
        RecyclerView.ViewHolder itemViewHolder = new GroupGameViewHolder(rootView);
        return itemViewHolder;
    }

    @Override
    public void subOnBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GroupGameViewHolder groupGameViewHolder = (GroupGameViewHolder) holder;
        final HMMatch hmMatch = (HMMatch) getTotal_data_list().get(position);
        LqswHealthMatch lqswHealthMatch = hmMatch.getLqswHealthMatch();
        List<Player> players = hmMatch.getPlayers();
        groupGameViewHolder.groupLeaderNameText.setText(lqswHealthMatch.getOwnerNickName());
        if (!TextUtils.isEmpty(lqswHealthMatch.getOwnerUserHead())) {
            Glide.with(mContext).load(lqswHealthMatch.getOwnerUserHead()).into(groupGameViewHolder.groupLeaderIcon);
        }else {
            groupGameViewHolder.groupLeaderIcon.setImageResource(R.drawable.user_head);
        }
        if (isNeedStep == 1) {
            groupGameViewHolder.centerImg.setImageResource(R.drawable.h_goal_icon);
        }

        int state = lqswHealthMatch.getState();//(1),未开始，(2)开始，(3)结束，(4)失效
        if (state == 1) {
            groupGameViewHolder.groupGameNameText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupGameDateText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupLeaderNameText.setTextColor(mContext.getResources().getColor(R.color.black));
            groupGameViewHolder.stateImg.setVisibility(View.INVISIBLE);
            groupGameViewHolder.groupGameStartText.setText("距离比赛开始还剩");
            groupGameViewHolder.groupGameStartDateText.setText(TimeHelper.getMyCountDownTime(lqswHealthMatch.getStartTime()));
            groupGameViewHolder.garyIcon.setVisibility(View.INVISIBLE);
            if (isNeedStep == 1) {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_goal_icon);
            } else {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_group_pk_icon);
            }
        } else if (state == 2) {
            groupGameViewHolder.groupGameNameText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupGameDateText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupLeaderNameText.setTextColor(mContext.getResources().getColor(R.color.black));
            groupGameViewHolder.stateImg.setVisibility(View.INVISIBLE);
            groupGameViewHolder.groupGameStartText.setText("距离比赛结束还剩");
            groupGameViewHolder.groupGameStartDateText.setText(TimeHelper.getMyCountDownTime(lqswHealthMatch.getEndTime()));
            groupGameViewHolder.garyIcon.setVisibility(View.INVISIBLE);
            if (isNeedStep == 1) {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_goal_icon);
            } else {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_group_pk_icon);
            }
        } else if (state == 3) {
            groupGameViewHolder.groupGameNameText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupGameDateText.setTextColor(mContext.getResources().getColor(R.color.blue_25));
            groupGameViewHolder.groupLeaderNameText.setTextColor(mContext.getResources().getColor(R.color.black));
            groupGameViewHolder.groupGameStartText.setText("");
            groupGameViewHolder.stateImg.setVisibility(View.VISIBLE);
            groupGameViewHolder.stateImg.setImageResource(R.drawable.h_group_finish);
            groupGameViewHolder.groupGameStartDateText.setText("");
            groupGameViewHolder.garyIcon.setVisibility(View.INVISIBLE);
            if (isNeedStep == 1) {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_goal_icon);
            } else {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.h_group_pk_icon);
            }
        } else if (state == 4) {
            groupGameViewHolder.groupGameStartText.setText("");
            groupGameViewHolder.stateImg.setVisibility(View.VISIBLE);
            groupGameViewHolder.stateImg.setImageResource(R.drawable.h_group_unfinish);

            groupGameViewHolder.groupGameNameText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.groupGameDateText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.groupGameStartText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.groupGameStartDateText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.groupGameStartDateText.setText("");
            groupGameViewHolder.groupLeaderNameText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.totalNumText.setTextColor(mContext.getResources().getColor(R.color.gray_97));
            groupGameViewHolder.garyIcon.setVisibility(View.VISIBLE);
            if (isNeedStep == 1) {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.score_game_no);
            } else {
                groupGameViewHolder.centerImg.setImageResource(R.drawable.group_game_no);
            }

        }

        groupGameViewHolder.groupGameNameText.setText(lqswHealthMatch.getMatchName());
        String betweenDate = TimeHelper.getDate_M_DByTimeStamp(lqswHealthMatch.getStartTime()) + "-" + TimeHelper.getDate_M_DByTimeStamp(lqswHealthMatch.getEndTime()) + TimeHelper.getDays(lqswHealthMatch.getStartTime(), lqswHealthMatch.getEndTime());
        groupGameViewHolder.groupGameDateText.setText(betweenDate);
        int m = 0;
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).getIsAccept() == 1) {
                m++;
            }
        }
        groupGameViewHolder.totalNumText.setText(m + "人");
        if (state == 2) {
            for (int i = 0; i < players.size(); i++) {
                if (players.get(i).getUserId() == UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    groupGameViewHolder.nowNumText.setText("当前排名:第" + players.get(i).getSort() + "名");
                }
            }
        } else if (state == 3) {
            for (int i = 0; i < players.size(); i++) {
                if (players.get(i).getUserId() == UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    groupGameViewHolder.nowNumText.setText("总排名:第" + players.get(i).getSort() + "名");
                }
            }
        } else {
            groupGameViewHolder.nowNumText.setText("");
        }
        groupGameViewHolder.groupGameItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(mContext, GroupPKDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("hMMatch", hmMatch);
                    intent.putExtras(bundle);
                    intent.putExtra("isNeedStep", isNeedStep);
                    mContext.startActivity(intent);
            }
        });

    }

    static class GroupGameViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.center_img)
        ImageView centerImg;
        @InjectView(R.id.group_game_item_button)
        RelativeLayout groupGameItemButton;
        @InjectView(R.id.group_game_name_text)
        TextView groupGameNameText;
        @InjectView(R.id.group_game_date_text)
        TextView groupGameDateText;
        @InjectView(R.id.group_game_start_text)
        TextView groupGameStartText;
        @InjectView(R.id.group_game_start_date_text)
        TextView groupGameStartDateText;
        @InjectView(R.id.group_leader_icon)
        RectImageView groupLeaderIcon;
        @InjectView(R.id.group_leader_name_text)
        TextView groupLeaderNameText;
        @InjectView(R.id.now_num_text)
        TextView nowNumText;
        @InjectView(R.id.total_num_text)
        TextView totalNumText;
        @InjectView(R.id.state_img)
        ImageView stateImg;
        @InjectView(R.id.gary_icon)
        ImageView garyIcon;

        GroupGameViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
